﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using community.LanFen;


namespace community
{
    public partial class publicity : Form
    {
        sql_database database = new sql_database();//引用sql类库里面的方法。
        
        public static int i = 0; //保存标志，1为新增后保存，0为修改后保存。

        public string strSql;
        string strConn = SQL.conStr;

        public publicity()
        {

            InitializeComponent();
        }

        protected override bool ProcessDialogKey(Keys keyData)//按enter键时，光标跳到下一个控件输入
        {
            if (keyData == Keys.Enter)//&& !(ActiveControl is Button))
            {//碰到Button时不跳转到下一个控件，而是执行Button的单击事件
                keyData = Keys.Tab;
            }
            return base.ProcessDialogKey(keyData);
        }

        //刷新
        private void shuaXin()
        {
            strSql = "select SEQID_INT as 序号 ,ACTIVITYTIME_DAT as 活动时间 , ACTIVITMAINPIONT_VCHR as 活动要点, ACTIVITYTHEME_VCHR as 活动主题 , ACTIVITYADDR_VCHR as 地点, ACTIVITYTYPE_VCHR as 活动形式, ORGANIZERS_VCHR as 主办单位 ,PARTNER_VCHR as 合作伙伴 ,RECORDERID_VCHR as 负责人,RECORDDATE_DAT as 填表时间 from  t_cs_healtheducation";
            database.show_dataGridview(strSql, "t_cs_healtheducation", dataGridView2);

            mouse();

        }

        private void publicity_Load(object sender, EventArgs e)
        {

            shuaXin();

        }

        //新增记录
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            i = 1;//标志增加类型的保存


            //清空textbox 
            dateTimePicker1.Value = DateTime.Now;
            themes.Clear();
            points.Clear();
            address.Clear();
            type.Text = "";
            partner.Clear();
            org.Clear();
            peoples_number.Clear();
            assign.Clear();
            sum.Clear();
            comment.Clear();
            recorder.Clear();
            dateTimePicker2.Value = DateTime.Now;
            //显示宣传册的输入链接
        }

        //保存操作
        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (i == 1)
            {
                strSql = "insert into t_cs_healtheducation (SEQID_INT, ACTIVITYTIME_DAT , ACTIVITYTHEME_VCHR , ACTIVITMAINPIONT_VCHR , ACTIVITYADDR_VCHR ,ACTIVITYTYPE_VCHR  , PARTNER_VCHR ,ORGANIZERS_VCHR , JOININNUM_VCHR , JIONASIGNMENT_VCHR , ACTIVITYSUMMARY_VCHR , ACTIVITYCOMMENT_VCHR , RECORDERID_VCHR , RECORDDATE_DAT) values ('";
                strSql += database.getSerial("t_cs_healtheducation", "SEQID_INT") + "','" + dateTimePicker1.Value + "','" + themes.Text.Trim() + "','" + points.Text.Trim() + "','" + address.Text.Trim() + "','";
                strSql += type.Text.Trim() + "','" + partner.Text.Trim() + "','" + org.Text.Trim() + "','" + peoples_number.Text.Trim() + "','" + assign.Text.Trim() + "','" + sum.Text.Trim() + "','" + comment.Text.Trim() + "','" + recorder.Text.Trim() + "','" + dateTimePicker2.Value + "')";
                //MessageBox.Show(strSql);

                database.new_row(strSql);

                //MessageBox.Show("");
                i = 0;
            }
            else//修改
            {
                string code = Convert.ToString(dataGridView2[0, dataGridView2.CurrentCell.RowIndex].Value).Trim();
                string sqlText = "update  t_cs_healtheducation set ACTIVITYTIME_DAT = '" + dateTimePicker1.Value + " ' , ACTIVITYTHEME_VCHR = '";
                sqlText += themes.Text.Trim() + "', ACTIVITMAINPIONT_VCHR = '" + points.Text.Trim() + "', ACTIVITYADDR_VCHR ='";
                sqlText += address.Text.Trim ()+"' ,ACTIVITYTYPE_VCHR = '" + type.Text.Trim() + "' , PARTNER_VCHR='" + partner.Text.Trim() + "' ,ORGANIZERS_VCHR='";
                sqlText += org.Text.Trim() + "' , JOININNUM_VCHR ='" + peoples_number.Text.Trim() + "', JIONASIGNMENT_VCHR='" + assign.Text.Trim() + "' , ACTIVITYSUMMARY_VCHR ='";
                sqlText += sum.Text.Trim ()+"' , ACTIVITYCOMMENT_VCHR=' "+comment.Text.Trim ()+"' , RECORDERID_VCHR='"+recorder.Text.Trim ()+"' , RECORDDATE_DAT ='"+dateTimePicker2.Value +"' where SEQID_INT ="+code ;
                //MessageBox.Show(sqlText);
                using (SqlConnection conn = new SqlConnection(SQL.conStr))
                {
                    if (conn.State == ConnectionState.Closed)
                        conn.Open();
                    SqlCommand com = new SqlCommand(sqlText, conn);

                    int logo = 0;
                    try
                    {
                        logo  = com.ExecuteNonQuery();
                        if (logo == 1)
                            MessageBox.Show("恭喜！修改成功！");
                        else
                        {

                            MessageBox.Show("修改失败！");
                        }
                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show(ex.ToString());
                    }
                    i = 0;
                }

            }
            //刷新
            shuaXin();


        }


        //弹出宣传册增加窗口。
        private void gridview1_label_Click(object sender, EventArgs e)
        {
            BrochureAdded brochure = new BrochureAdded();
            brochure.ShowDialog();

        }

        private void mouse()
        {
            string code = Convert.ToString(dataGridView2[0, dataGridView2.CurrentCell.RowIndex].Value).Trim();
            if (code=="")
            {
                return;
            }
            string strSql = "select SEQID_INT, ACTIVITYTIME_DAT , ACTIVITYTHEME_VCHR , ACTIVITMAINPIONT_VCHR , ACTIVITYADDR_VCHR ,ACTIVITYTYPE_VCHR  , PARTNER_VCHR ,ORGANIZERS_VCHR , JOININNUM_VCHR , JIONASIGNMENT_VCHR , ACTIVITYSUMMARY_VCHR , ACTIVITYCOMMENT_VCHR , RECORDERID_VCHR , RECORDDATE_DAT from  t_cs_healtheducation where SEQID_INT ='" + code + "'";
            //MessageBox.Show(strSql);
            using (SqlConnection conn = new SqlConnection(SQL.conStr))
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();

                SqlCommand com = new SqlCommand(strSql, conn);

                try
                {
                    SqlDataReader rdr = com.ExecuteReader();

                    while (rdr.Read())
                    {
                        dateTimePicker1.Value = Convert.ToDateTime(rdr["ACTIVITYTIME_DAT"]);
                        themes.Text = rdr["ACTIVITYTHEME_VCHR"].ToString();
                        points.Text = rdr["ACTIVITMAINPIONT_VCHR"].ToString();
                        address.Text = rdr["ACTIVITYADDR_VCHR"].ToString();
                        type.Text = rdr["ACTIVITYTYPE_VCHR"].ToString();
                        partner.Text = rdr["PARTNER_VCHR"].ToString();
                        org.Text = rdr["ORGANIZERS_VCHR"].ToString();
                        peoples_number.Text = rdr["JOININNUM_VCHR"].ToString();
                        assign.Text = rdr["JIONASIGNMENT_VCHR"].ToString();
                        sum.Text = rdr["ACTIVITYSUMMARY_VCHR"].ToString();
                        comment.Text = rdr["ACTIVITYCOMMENT_VCHR"].ToString();
                        recorder.Text = rdr["RECORDERID_VCHR"].ToString();
                        dateTimePicker2.Value = Convert.ToDateTime(rdr["RECORDDATE_DAT"]);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }


            }
            //显示宣传资料信息
            string s = "select ID  , brochure_name as 宣传册名称 ,brochure_account as 数量 ,brochure_content as 内容 ,addDate_dat as 修改时间 , publicity_ID as 活动ID  from brochure where publicity_ID = " + code;
            database.show_dataGridview(s, "brochure", dataGridView1); 
        }

        //鼠标事件
        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            mouse();
        }
        //删除功能
        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            //是否删除
            if (MessageBox.Show("确定删除改记录吗？", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                return;
            

            string code = Convert.ToString(dataGridView2[0, dataGridView2.CurrentCell.RowIndex].Value).Trim();

            if (code == "")
            {
                return;
            }
            string sqltext = "delete from  T_CS_HEALTHEDUCATION where SEQID_INT = " + code + "; delete from brochure where publicity_ID ="+code;



            using (SqlConnection  conn = new SqlConnection(SQL.conStr  ))
            {
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                SqlCommand com = new SqlCommand(sqltext, conn);

                int i=0;
                try
                {
                    i = com.ExecuteNonQuery();
                    if (i == 0)
                        MessageBox.Show("删除失败！");
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.ToString());
                }


            }
            shuaXin();

        }
    }
}
